import axios from "axios";
import { refreshToken } from './sercer'

axios.defaults.baseURL = "http://127.0.0.1:3000";

//请求拦截
axios.interceptors.request.use(function (config) {
    //请求发送前
    let accessToken = localStorage.getItem('accessToken')
    let refreshToken = localStorage.getItem('refreshToken')
    //设置请求头
    config.headers.accessToken = accessToken
    config.headers.refreshToken = refreshToken
    return config;
}, function (error) {
    //请求发送失败
    return Promise.reject(error);
});

//响应拦截
axios.interceptors.response.use(function (response) {
    if (response.data.code === 401) {
        refreshToken() //刷新token 短token过期后用长token刷新短token
    }
    return response;
}, function (error) {
    //响应失败
    return Promise.reject(error);
});
axios.interceptors.response.use(config => {
    return config.data
})

export default axios;